package module01_work;

import java.util.Iterator;
import java.util.LinkedHashSet;

import org.junit.Test;

/**
 * 
 * @author 陈洛健 目的： 找出1000以内的所有完数并打印出来。 main()中 一开始没想到使用% ，利用整型跟浮点型的强制转换会丢失小数点来比较
 *         ，相除后的结果是否是整数。 
 * 
 */

public class Work02 {

	public static void main(String[] args) {
		
		LinkedHashSet<Object> set = new LinkedHashSet<>();
//		ArrayList<Integer> list = new ArrayList<>();
		int sum = 0;
		double result = 0;
		
		for (int i = 1; i <= 1000; i++) {
			sum = 0;
			for (int j = 1; j < i; j++) {
				if ((result = 1.0 * i / j) > 0 && (int) result == result) {
					sum += j;
				}
			}
			if (i == sum) {
				set.add(i);
			}

		}
		
		Iterator<Object> iterator = set.iterator();

		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

	/*	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList<>();

		int temp = 0, count = 0;
		double result = 0;
		for (int i = 1; i <= 1000; i++) {
			// 将i赋值给temp 作为要查找原数的数 count = 0; temp = i; 
			for (int j = 1; j < temp; j++) {
			if ((result = 1.0 * i / j) > 0 && (int) result == result) {
				count += j;
			}
		}
		if (count == temp) {
			list.add(i);
		}

		ListIterator<Integer> listIterator = list.listIterator();

		while (listIterator.hasNext()) {
			System.out.println(listIterator.next());
		}
	}*/

	@Test
	public void test01() {
		long begin = System.currentTimeMillis();
		int sum = 0;

		for (int i = 1; i <= 1000000; i++) {
			sum = 0;
			for (int j = 1; j < i; j++) {
				if (i % j == 0) {
					sum += j;
				}
			}
			if (i == sum) {
				System.out.println("完数是：" + i);
			}

		}
		long end = System.currentTimeMillis();
		System.out.println("使用的时间为：" + (end-begin));
	}

}
